package com.example.SpringBean;
import com.example.SpringBean.model.Coffee;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;

@SpringBootApplication
@Slf4j
public class SpringBeanApplication implements ApplicationRunner {
	@Autowired
	private Coffee coffee1;
	@Resource
	private Coffee coffee2;
	@Autowired
	@Qualifier(value = "coffee")
	private Coffee coffee3;

	public static void main(String[] args) {
		SpringApplication.run(SpringBeanApplication.class, args);
	}
	@Override
	public void run(ApplicationArguments args) throws Exception {
		//Autowired
		log.info("Autowired：" + coffee1.toString());
//		Resource
		log.info("Resource：" + coffee2.toString());
		//xml
		log.info("Xml：" + getCoffeeByXml().toString());
		//spring
		log.info("spring：" + getCoffeeByAnnotation().toString());
		//
		log.info("qualifier" + coffee3.toString());
	}
	//xml注入bean
	private Coffee getCoffeeByXml()  {
		ApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/bean.xml");
		Coffee coffee = xmlApplicationContext.getBean("coffeeFromBean", Coffee.class);
		return coffee;
	}

	//spring注入bean
	private Coffee getCoffeeByAnnotation() {
		ApplicationContext context = new AnnotationConfigApplicationContext(Coffee.class);
		Coffee coffee = context.getBean("coffee", Coffee.class);
		return coffee;
	}
}
